/*
    BibleNotebook - A bible reader software
    Copyright (C) 2014  Williams Fernando Lopez

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
 */

/*
 * NotificationPane.java
 *
 * Created on 8/07/2013, 09:57:30 AM
 */

package com.simplebible.ui.components;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;
import wl.util.TextResourcesUtil;

/**
 *
 * @author Williams Lopez
 */
public class NotificationPane extends javax.swing.JPanel {


    public static final int TYPE_ERROR = 1;
    public static final int TYPE_INFO = 2;
    public static final int TYPE_WAITING = 3;
    public static final int TYPE_QUESTION = 4;


    private Component invoker = null;
    private int x, y;
    private JPopupMenu popup;

    private ImageIcon iconError;
    private ImageIcon iconInfo;
    private ImageIcon iconWaiting;

    private List<YesNoCancelListener> yesNoCancelListener ;

    private String shortDescription = null;
    private String longDescription = null;


    /** Creates new form NotificationPane */
    public NotificationPane() {
        initComponents();
        popup = new JPopupMenu();
        popup.add(this);
        popup.setBorder(BorderFactory.createEmptyBorder());
        iconError = new javax.swing.ImageIcon(getClass().getResource("/simplebible/ui/img/Error.png"));
        iconInfo  = new javax.swing.ImageIcon(getClass().getResource("/simplebible/ui/img/info.png"));
        iconWaiting = new javax.swing.ImageIcon(getClass().getResource("/simplebible/ui/img/searching-1.gif"));
    }


    public void setLocation(Component invoker, int x, int y){
        this.invoker = invoker;
        this.x = x;
        this.y = y;
    }

    public void show(String shortDesc, String longDesc, int type){

        this.shortDescription = shortDesc;
        this.longDescription = longDesc;

        switch(type){
            case TYPE_ERROR:
                jLabel1.setIcon(iconError);
                this.buttonYes.setVisible(false);
                this.buttonNo.setVisible(false);
                break;
            case TYPE_INFO:
                jLabel1.setIcon(iconInfo);
                this.buttonYes.setVisible(false);
                this.buttonNo.setVisible(false);
                break;
            case TYPE_WAITING:
                jLabel1.setIcon(iconWaiting);
                this.buttonYes.setVisible(false);
                this.buttonNo.setVisible(false);
                break;
            case TYPE_QUESTION:
                jLabel1.setIcon(iconInfo);
                this.buttonYes.setVisible(true);
                this.buttonNo.setVisible(true);
        }

        this.jLabel1.setText("<html><div>"+shortDescription+"</div>");
        int fx = (int)(this.invoker.getLocationOnScreen().getX()+x);
        int fy = (int)(this.invoker.getLocationOnScreen().getY()+y);

        if(fx<0){
            fx = 0;
        }

        if(fy<0){
            fy=0;
        }

        if(longDescription==null || longDescription.trim().equals("")||longDescription.equals(shortDescription)){
            labelInfo.setVisible(false);
        }else{
            labelInfo.setVisible(true);
        }

        popup.setLocation(fx,fy);
        this.setVisible(true);
    }


    private void showLongDescription() {
        InfoViewerDialog.showDialog(TextResourcesUtil.getText("info"),longDescription);
    }


    @Override
    public void setVisible(boolean aFlag) {
        super.setVisible(aFlag);
        popup.setVisible(aFlag);
    }

    public JPopupMenu getPopup(){
        return popup;
    }

    public List<YesNoCancelListener> getYesNoCancelListener() {
        return yesNoCancelListener;
    }

    private void setYesNoCancelListener(List<YesNoCancelListener> yesNoCancelListener) {
        this.yesNoCancelListener = yesNoCancelListener;
    }

    public void addYesNoCancelListener(YesNoCancelListener listener){
        if(this.yesNoCancelListener==null){
            this.yesNoCancelListener = new ArrayList<YesNoCancelListener>();
        }
        this.yesNoCancelListener.add(listener);
    }

    private void fireYesListener(){
        if(yesNoCancelListener!=null){
            for(YesNoCancelListener y: yesNoCancelListener){
                y.yes(new ActionEvent(this, 0, "yes"));
            }
        }
    }

    private void fireNoListener(){
        if(yesNoCancelListener!=null){
            for(YesNoCancelListener y: yesNoCancelListener){
                y.no(new ActionEvent(this, 0, "no"));
            }
        }
    }

    private void fireCancelListener(){
        if(yesNoCancelListener!=null){
            for(YesNoCancelListener y: yesNoCancelListener){
                y.cancel(new ActionEvent(this, 0, "cancel"));
            }
        }
    }


    public void clear() {
        if(yesNoCancelListener!=null)
            yesNoCancelListener.clear();
        this.jLabel1.setText("");
        shortDescription = null;
        longDescription = null;
    }








    

    
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        labelClose = new javax.swing.JLabel();
        labelInfo = new javax.swing.JLabel();
        buttonYes = new javax.swing.JButton();
        buttonNo = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simplebible/ui/img/info.png"))); // NOI18N
        jLabel1.setText(" <html>\n<head>\n</head>\n<body>\nAplicaci&oacute;n iniciada correctamente</br>\nWFLC @copyright. All rights reserved\n</body>\n</html>");
        jLabel1.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel1MouseClicked(evt);
            }
        });

        labelClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simplebible/ui/img/delete_16.png"))); // NOI18N
        labelClose.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        labelClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                labelCloseMouseClicked(evt);
            }
        });

        labelInfo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/simplebible/ui/img/info_16.png"))); // NOI18N
        labelInfo.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        labelInfo.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                labelInfoMouseClicked(evt);
            }
        });

        buttonYes.setText("Yes");
        buttonYes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonYesActionPerformed(evt);
            }
        });

        buttonNo.setText("No");
        buttonNo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonNoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 374, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelInfo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(labelClose))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buttonYes, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonNo)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelClose)
                    .addComponent(labelInfo))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonNo)
                    .addComponent(buttonYes))
                .addGap(34, 34, 34))
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jLabel1MouseClicked

    private void labelCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelCloseMouseClicked
        fireCancelListener();
        this.setVisible(false);
    }//GEN-LAST:event_labelCloseMouseClicked

    private void labelInfoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_labelInfoMouseClicked
        showLongDescription();
    }//GEN-LAST:event_labelInfoMouseClicked

    private void buttonYesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonYesActionPerformed
        fireYesListener();
        setVisible(false);
    }//GEN-LAST:event_buttonYesActionPerformed

    private void buttonNoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonNoActionPerformed
        fireNoListener();
        setVisible(false);
    }//GEN-LAST:event_buttonNoActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonNo;
    private javax.swing.JButton buttonYes;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel labelClose;
    private javax.swing.JLabel labelInfo;
    // End of variables declaration//GEN-END:variables





}
